/*==========模型==========*/
Ext.define('Tg.tx.CURDViewController', {
	extend: 'Ext.app.ViewController',
	
    //验证权限
	expandPath:function(_V,_Data){
		var _Me = this;
		var _VM = _V.getViewModel();
		var _Tree = _V.down('treepanel');
		if(_Data == null){
			var _Tree = _V.down('treepanel');
			var rootNode = _Tree.getRootNode();
			_Data = rootNode.childNodes;
		}
		var _PPk = _VM.get('_PPk');
		var _PPKey = _VM.get('_PPKey');
		for(var i=0;i<_Data.length;i++){
			if(_Data[i]['data'][_PPk] == _PPKey){
				_Data[i].expand();
				return _Data[i];
			}else{
				var childNodes = _Data[i].childNodes;
				 if(childNodes.length>0){
					var R = _Me.expandPath(_V,childNodes);
				 }
			}
		}
		return false;
	},
    //验证权限
	_CheckAuth:function(_A){
		var _Me = this;
		var _VM = _Me.getView().getViewModel();
		var _Auth = _VM.get('_Auth.'+_A);
		if(!_Auth){
			Ext.Msg.alert('错误', '你没有权限进行此操作!');
			return false;
		}else{
			return true;
		}
	},
    //获取列表类型
	_ListType:function(_View){
		var _Grid = _View.down('gridpanel');
		if(!_Grid){
			_Grid = _View.down('treepanel');
		}
		return _Grid;
	},
	_ColumnSelect:function(_Grid,_Index){
        _Grid.selModel.deselect(_Grid.selModel.getSelection());
		_Grid.selModel.select(_Index,true);
	},
    //根据grid控制顶部按钮显示隐藏
    _SelectChange:function(model, selected, eOpts){
		var _Me = this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		if(_VM.get('_Auth.edit') && _V.down('toolbar button#_Edit')){
    		_V.down('toolbar button#_Edit')[selected.length == 1 ? 'enable' : 'disable']();
		}
		if(_VM.get('_Auth.look') && _V.down('toolbar button#_Look')){
    		_V.down('toolbar button#_Look')[selected.length == 1 ? 'enable' : 'disable']();
		}
		if(_VM.get('_Auth.put') && _V.down('toolbar button#_Put')){
    		_V.down('toolbar button#_Put')[selected.length > 0 ? 'enable' : 'disable']();
		}
		if(_VM.get('_Auth.del') && _V.down('toolbar button#_Del')){
    		_V.down('toolbar button#_Del')[selected.length > 0  ? 'enable' : 'disable']();
		}
		//子类继承此方法必须执行
		//this.callParent(); 
    },
    //刷新
	_Reload:function(){
		var _Me = this;
		var _V	= _Me.getView();
		var _VM = _V.getViewModel();
		var _Store =_VM.get('_MainStore');
		if(_Store.type == 'tree'){
			_Store.reload({    
			  callback : function(r, options, success) {    
					if (success == true) {
						_Me.expandPath(_V,null);
					}
				}
			});
		}else{
			_Store.reload();
		}
	},
    //搜索
	_Serch:function(button, e, eOpts){
		var _Me = this;
		var _VM = _Me.getView().getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Ext = _VM.get('_Ext.serch');
        var _Win = Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'搜索',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						var _Return	= Ext.create('Tg.tx.Action').searchAction({params:_Params,store:_Store});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'重置',
                    handler:function(_ResetBtn){
                        _ResetBtn.up("window").down("form").reset();
                    }
                }
            ]})
        });
	},
    //新增
	_Add:function(button, e, eOpts){
		var _Me		= this;
		if(!_Me._CheckAuth('add')){return false;}
		var _VM		= _Me.getView().getViewModel();
		var _Auth	= _VM.get('_Auth.add');
		var _Store	= _VM.get('_MainStore');
		var _Url	= _VM.get('_Url.add');
		var _Ext	= _VM.get('_Ext.add');
        var _Win	= Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						if(_Store.type == 'tree'){
							if(_Params[_VM.get('_PPk')]){
								_VM.set('_PPKey',_Params[_VM.get('_PPk')]);
							}
						}
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store,me:_Me});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		Ext.create('Tg.tx.Action').getDataToForm({url:_Url,form:_Win.down("form").getForm()})
	},
    //更新
	_Edit:function(button, e, eOpts){
		var _Me	= this;
		if(!_Me._CheckAuth('edit')){return false;}
		var _V		= _Me.getView();
		var _VM		= _V.getViewModel();
		var _Store	= _VM.get('_MainStore');
		var _Url	= _VM.get('_Url.edit');
		var _Ext	= _VM.get('_Ext.edit');
		var _Pk		= _VM.get('_Pk');
		var _Grid	= _Me._ListType(_V);
        var _Select	=_Grid.selModel.getSelection();
        if (_Select.length < 1) {
            Ext.MessageBox.alert('提示', '请最少选择一条记录后操作');
			return false;
        } else if (_Select.length > 1) {
            Ext.MessageBox.alert('提示', '一次只能编辑一条记录');
			return false;
        }
        var _Win = Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						if(_Store.type == 'tree'){
							if(_Params[_VM.get('_PPk')]){
							_VM.set('_PPKey',_Params[_VM.get('_PPk')]);
							}
						}
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store,me:_Me});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		var _Params={};
		_Params[_Pk]=_Select[0].get(_Pk);
		Ext.create('Tg.tx.Action').getDataToForm({params:_Params,url:_Url,form:_Win.down("form").getForm()})
	},
    //查看
	_Look:function(button, e, eOpts){
		var _Me		= this;
		if(!_Me._CheckAuth('look')){return false;}
		var _V		= _Me.getView();
		var _VM		= _V.getViewModel();
		var _Store	= _VM.get('_MainStore');
		var _Url	= _VM.get('_Url.look');
		var _Ext	= _VM.get('_Ext.look');
		var _Pk		= _VM.get('_Pk');
		var _Grid	= _Me._ListType(_V);
        var _Select	=_Grid.selModel.getSelection();
        if (_Select.length < 1) {
            Ext.MessageBox.alert('提示', '请最少选择一条记录后操作');
			return false;
        } else if (_Select.length > 1) {
            Ext.MessageBox.alert('提示', '一次只能查看一条记录');
			return false;
        }
        var _Win = Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'关闭',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		var _Params={};
		_Params[_Pk]=_Select[0].get(_Pk);
		Ext.create('Tg.tx.Action').getDataToForm({params:_Params,url:_Url,form:_Win.down("form").getForm()})
	},
    //打印
    _Put:function(button, e, eOpts){
		//TODO
    },
    //删除
    _Del:function(button, e, eOpts){
		var _Me		= this;
		if(!_Me._CheckAuth('del')){return false;}
		var _V		= _Me.getView();
		var _VM		= _V.getViewModel();
		var _Store	= _VM.get('_MainStore');
		var _Url	= _VM.get('_Url.del');
		var _Pk		= _VM.get('_Pk');
		var _Grid	= _Me._ListType(_V);
        var _Select	=_Grid.selModel.getSelection();
        if (_Select.length < 1) {
            Ext.MessageBox.alert('提示', '请最少选择一条记录后删除');
			return false;
        } else {
            _Msg = '确定要删除选中的 ' + _Select.length + ' 条记录?';
        }
		var _Params={};
		_Params[_Pk]=_TStC(_Select);
		if(_Store.type == 'tree'){
			if(_Select[0].get(_VM.get('_PPk'))){
				_VM.set('_PPKey',_Select[0].get(_VM.get('_PPk')));
			}
		}
		_Return	= Ext.create('Tg.tx.Action').AjaxAction({title:'删除确认',msg:'请问您,是否确认删除这些数据？',params:_Params,url:_Url,store:_Store,me:_Me});
    },
    //行_编辑
    _ColumnEdit:function(_Grid, _Rindex, _Cindex) {
		var _Me		= this;
		var _V		= _Me.getView();
		_Me._ColumnSelect(_Grid,_Rindex);
		_V.down('toolbar button#_Edit').click();
    },
    //行_查看
    _ColumnLook:function(_Grid, _Rindex, _Cindex) {
		var _Me		= this;
		var _V		= _Me.getView();
		_Me._ColumnSelect(_Grid,_Rindex);
		_V.down('toolbar button#_Look').click();
    },
    //行_打印
    _ColumnPut:function(_Grid, _Rindex, _Cindex) {
		//TODO
    },
    //行_删除
    _ColumnDel:function(_Grid, _Rindex, _Cindex) {
		var _Me		= this;
		var _V		= _Me.getView();
		_Me._ColumnSelect(_Grid,_Rindex);
		_V.down('toolbar button#_Del').click();
    },
	//行双击
	_ItemDbClick:function(_Obj,_Record,item,index,e,eOpts ){
		var _Me		= this;
		if(!_Me._CheckAuth('edit')){return false;}
		var _V		= _Me.getView();
		var _VM		= _V.getViewModel();
		var _Grid	= _Me._ListType(_V);
		_Me._ColumnSelect(_Grid,index);
		_V.down('toolbar button#_Edit').click();
	}
});